package com.jinyou.utils.common;

import java.util.*;
import java.util.Map.Entry;

public class ToolUtils {

    private final static int tokenLength = 24; // 随机access_token的长度
    private final static StringBuffer buffer = new StringBuffer(
            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    private final static StringBuffer numbers = new StringBuffer("0123456789");

    // 随机生成登录用户的access_token,作为用户的登录凭证
    public static String getToken() {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        int range = buffer.length();
        for (int i = 1; i <= tokenLength; i++) {
            sb.append(buffer.charAt(random.nextInt(range)));
        }
        return sb.toString();
    }

    public static String getNeedString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        int range = buffer.length();
        for (int i = 1; i <= length; i++) {
            sb.append(buffer.charAt(random.nextInt(range)));
        }
        return sb.toString();
    }

    // 随机生成6位手机验证码
    public static String getTelCode() {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        int range = numbers.length();
        for (int i = 1; i <= 6; i++) {
            sb.append(buffer.charAt(random.nextInt(range)));
        }
        return sb.toString();
    }


    // 随机生成12位验证码
    public static String getVerifyCode() {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        int range = numbers.length();
        for (int i = 1; i <= 12; i++) {
            sb.append(buffer.charAt(random.nextInt(range)));
        }
        return sb.toString();
    }

    // 验证字符串是否为数字
    // 是，返回true； 不是，返回false
    public static boolean isNumber(String value) {
        return isNumber(value);
    }

    // 将String类型转换成int[]
    public static String[] String2StringArray(String str) {
        String[] statics = null;
        if (str.equals("")) {
            return null;
        }
        if (str.indexOf(",") > 0) {
            String[] staStr = str.split(",");
            statics = new String[staStr.length];
            for (int i = 0; i < staStr.length; i++) {
                statics[i] = staStr[i];
            }
        } else {
            statics = new String[1];
            statics[0] = str;
        }
        return statics;
    }

    // 将list的内容装换成String
    public static String List2LongArray(List<HashMap<String, Object>> list) {
        if (list != null && list.size() > 0) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                for (Entry<String, Object> entry : map.entrySet()) {
                    System.out.println("Key = " + entry.getKey() + ", Value = "
                            + entry.getValue());
                    if (i == list.size() - 1) {
                        sb.append(entry.getValue());
                    } else {
                        sb.append(entry.getValue()).append(",");
                    }
                }
            }
            return sb.toString();
        } else {
            return null;
        }
    }

    // 将String类型转换成int[]
    public static long[] String2LongArray(String str) {
        long[] statics = null;
        if (str.indexOf(",") > 0) {
            String[] staStr = str.split(",");
            statics = new long[staStr.length];
            for (int i = 0; i < staStr.length; i++) {
                statics[i] = Long.parseLong(staStr[i]);
            }
        } else {
            statics = new long[1];
            statics[0] = Integer.parseInt(str);
        }
        return statics;
    }

    //添加几个0
    public static String getSize0(Object id, int size) {
        String idStr = String.valueOf(id);
        if (idStr.length() >= size) {
            return idStr;
        }
        return getSize0(size - idStr.length()) + idStr;
    }

    private static String getSize0(int size) {
        StringBuffer sb = new StringBuffer();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                sb.append("0");
            }
        }
        return sb.toString();
    }

    //比较两个字符串的大小
    public static int StringCompare(String str1, String str2) {
        if (str1 == null || str2 == null || str1.equals("") || str2.equals("")) {
            return -1;
        } else {
            if (str1.equals(str2)) {
                return 0;
            }
            int length = Math.min(str1.length(), str2.length());
            for (int i = 0; i < length; i++) {
                if (str1.charAt(i) == str2.charAt(i)) {
                    continue;
                } else {
                    int m = length - i - 1;
                    String n = getSize0(Math.abs(str1.charAt(i) - str2.charAt(i)), 2);
                    return Integer.parseInt(String.valueOf(m) + String.valueOf(n));
                }
            }
        }
        return 1;
    }

    //将list按照size返回所需多少
    public static List<? extends Object> list(List<? extends Object> listp, int start, int end) {
        if (listp == null || listp.size() < 1 || end < start || start < 1 || start > listp.size()) {
            return null;
        }
        List<Object> list = new ArrayList<Object>();
        for (int i = start - 1; i < end && i <= listp.size() - 1; i++) {
            list.add(listp.get(i));
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(StringCompare("yyybq", "yyyam"));
    }

}
